package com.sh.rbac.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.rbac.annotation.OperationLogger;
import com.sh.rbac.domain.dto.param.ParamQueryDTO;
import com.sh.rbac.domain.dto.param.ParamSaveDTO;
import com.sh.rbac.domain.dto.param.ParamUpdateDTO;
import com.sh.rbac.domain.entity.Param;
import com.sh.rbac.domain.enums.OperationTypeEnum;
import com.sh.rbac.result.Result;
import com.sh.rbac.service.ParamService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 参数控制器
 *
 * @author lucky
 * @since 2024-06-26
 */

@Tag(name = "参数管理", description = "参数相关操作")
@RequestMapping("sys/param")
@RestController
public class ParamController {

    @Autowired
    private ParamService paramService;

    @Operation(summary = "查询参数列表")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Param.class)))
    @GetMapping("list")
    @PreAuthorize("hasAuthority('sys:param:list')")
    public Result<List<Param>> getList() {
        return Result.success(paramService.list());
    }


    @Operation(summary = "分页查询参数列表", description = "根据参数条件查询参数进行查询")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Param.class)))
    @PostMapping("page/{current}/{limit}")
    @PreAuthorize("hasAuthority('sys:param:page')")
    public Result<Page<Param>> getPage(@PathVariable Long current, @PathVariable Long limit, @RequestBody ParamQueryDTO paramQueryDTO) {
        return Result.success(paramService.getPage(current, limit, paramQueryDTO));
    }


    @OperationLogger(module = "系统管理->参数管理", type = OperationTypeEnum.ADD)
    @Operation(summary = "添加参数", description = "保存一个新参数")
    @ApiResponse(responseCode = "200", description = "成功保存参数")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('sys:param:save')")
    public Result<Boolean> save(@RequestBody ParamSaveDTO paramSaveDTO) {
        return paramService.save(paramSaveDTO) ? Result.success() : Result.failure();
    }


    @OperationLogger(module = "系统管理->参数管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新参数", description = "根据ID更新参数信息")
    @ApiResponse(responseCode = "200", description = "成功更新参数")
    @PutMapping("update")
    @PreAuthorize("hasAuthority('sys:param:update')")
    public Result<Boolean> update(@RequestBody ParamUpdateDTO paramUpdateDTO) {
        return paramService.updateById(paramUpdateDTO) ? Result.success() : Result.failure();
    }

    @OperationLogger(module = "系统管理->参数管理", type = OperationTypeEnum.DELETE)
    @Operation(summary = "删除参数", description = "根据ID删除参数信息")
    @ApiResponse(responseCode = "200", description = "成功删除参数")
    @DeleteMapping("delete/{id}")
    @PreAuthorize("hasAuthority('sys:param:delete')")
    public Result<Boolean> delete(@PathVariable Long id) {
        return paramService.removeById(id) ? Result.success() : Result.failure();
    }

    @OperationLogger(module = "系统管理->参数管理", type = OperationTypeEnum.BATCH_DELETE)
    @Operation(summary = "批量删除参数", description = "批量删除参数信息")
    @ApiResponse(responseCode = "200", description = "成功删除参数")
    @DeleteMapping("batch-delete")
    @PreAuthorize("hasAuthority('sys:param:batch-delete')")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return paramService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }


    @Operation(summary = "根据ID查询参数")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = Param.class)))
    @GetMapping("get/{id}")
    @PreAuthorize("hasAuthority('sys:param:get')")
    public Result<Param> get(@PathVariable Long id) {
        return Result.success(paramService.getById(id));
    }


    @OperationLogger(module = "系统管理->参数管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新参数状态", description = "根据ID更新参数状态")
    @ApiResponse(responseCode = "200", description = "成功更新参数状态")
    @PutMapping("update-status/{id}/{status}")
    @PreAuthorize("hasAuthority('sys:param:update-status')")
    public Result<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        return paramService.updateStatus(id, status) ? Result.success() : Result.failure();
    }
}
